<template>
  <el-dialog :title="title"
             top="24vh"
             append-to-body
             class="dialog-box"
             :before-close="handleClose"
             :visible.sync="visible"
             :width="width">
    <el-form ref="ruleForm"
             :model="ruleForm"
             :rules="rules"
             label-width="110px"
             class="demo-ruleForm">
      <el-form-item label="邮箱"
                    prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <br />
      <el-form-item label="邮箱验证码"
                    prop="mailCode"
                    label-width="110px">
        <div class="item-flex">
          <el-input v-model="ruleForm.mailCode"
                    class="verificationSty"></el-input>
          <div style="
              height: 20px;
              border-left: 1px solid var(--prev-border-color-grey);
              margin-top: 8px;
            " />
          <el-button v-if="isShowMsgCode"
                     :disabled="!ruleForm.email"
                     style="color: var(--prev-color-primary); font-size: 14px; border: 0px"
                     @click="sendCode">发送验证码</el-button>
          <el-button v-if="!isShowMsgCode"
                     style="color: var(--prev-color-primary); font-size: 14px; border: 0px"
                     disabled>{{ showTime }}秒后重新发送</el-button>
        </div>
      </el-form-item>
    </el-form>
    <div slot="footer"
         class="dialog-footer">
      <el-button type="default"
                 @click="handleClose">取消</el-button>
      <el-button type="primary"
                 @click="container()">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { tenant } from "@/api/index";
import authentication from "@/libs/authentication";
export default {
  name: "EmailBind",
  mixins: [authentication],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    width: {
      type: String,
      default: "466px"
    },
    title: {
      type: String
    }
  },
  data () {
    return {
      isShowMsgCode: true,
      ruleForm: {
        email: "",
        mailCode: ""
      },
      rules: {
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          { min: 1, max: 50, message: "不能超50过个字符", trigger: "blur" },
          {
            pattern:
              /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/,
            message: "请输入正确的邮箱格式"
          }
        ],
        mailCode: [
          { required: true, message: '验证码不能为空', trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    async sendCode () {
      this.sendOutTime();
      let { code, message } = await tenant.mailCode({
        email: this.ruleForm.email
      });

      this.$message[code === 0 ? "success" : "error"](message);
    },
    handleClose () {
      this.$emit("update:visible", false);
    },
    container () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.bindEmail();
        }
      });
    },
    async bindEmail () {
      let { code, message } = await tenant.bindEmail(this.ruleForm);
      if (code === 0) {
        this.handleClose();
        this.$emit("EmailBind");
      }
      this.$message[code === 0 ? "success" : "error"](message);
    }
  }
};
</script>

<style lang="scss" scoped>
.dialog-box >>> .el-dialog__body {
  max-height: 460px !important;
  overflow: auto !important;
}
.el-form {
  .el-input {
    width: 300px;
    margin-right: 10px;
  }
}
.item-flex {
  display: flex;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  width: 300px;
}
.verificationSty {
  /deep/ .el-input__inner {
    border: 0px;
  }
}
</style>
